import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

import { BaseHttpService } from '../base-http.service';
import { CardInfo } from '../card-info/card-info.service';

export interface ImportResult {
  id: string;
  isupdating: boolean;
  status: Status;
}

export interface Status {
  currenttableresult: CurrentTableResult;
  failed: boolean;
  finished: boolean;
  key: string;
  processedrowcount: number;
}

export interface CurrentTableResult {
  errors: string[];
  failedcount: number;
  finished: boolean;
  index: number;
  insertedcount: number;
  messages: string[];
  name: string;
  processedcount: number;
  skippedcount: number;
  totalsourcecount: number;
}

@Injectable({
  providedIn: 'root'
})
export class OperationService {
  apiPrefix: string = 'Operation';
  constructor(private readonly http: BaseHttpService) {}

  public getOperationResult(id: string): Observable<ImportResult[]> {
    return this.http.get<ImportResult[]>(`/${this.apiPrefix}/${id}`);
  }

  public getCardInfos(id: number): Observable<CardInfo[]> {
    return this.http.get<CardInfo[]>(`/${this.apiPrefix}/getCardInfos?id=${id}`);
  }

  public validate(cardNumber: string, cardPassword: string): Observable<string> {
    return this.http.get<string>(`/${this.apiPrefix}/validateUsed?cardNumber=${cardNumber}&cardPassword=${cardPassword}`);
  }
}
